Component({
  properties: {
    percentage: {
      type: Number,
      value: 0,
    },
    time: {
      type: Number,
      value: 0
    }
  },

  data: {
    // 组件内部数据
    currentIndex: 1,
    currentHeaderIndex: 1,
    currentList: {
      jjjNum: null,
      twNum: null,
      jwNum: null,
      ywjbNum: null,
      ywtbNum: null,
      cwjbNum: null,
      cwtbNum: null,
    },
    list: [{
        img: '../../assets/settings/measure/pillow_settings_scientific_calculation_height_soft_unclicked.png',
        imgCheck: '../../assets/settings/measure/pillow_settings_scientific_calculation_height_soft_clicked.png',
        select: '../../assets/settings/measure/pillow_settings-unclicked.png',
        selected: '../../assets/settings/measure/pillow_settings-clicked.png',
        text: '较软',
        index: 0,
      },
      {
        img: '../../assets/settings/measure/pillow_settings_scientific_calculation_height_medium_unclicked.png',
        imgCheck: '../../assets/settings/measure/pillow_settings_scientific_calculation_height_medium_clicked.png',
        select: '../../assets/settings/measure/pillow_settings-unclicked.png',
        selected: '../../assets/settings/measure/pillow_settings-clicked.png',
        text: '中等',
        index: 1,
      },
      {
        img: '../../assets/settings/measure/pillow_settings_scientific_calculation_height_hard_unclicked.png',
        imgCheck: '../../assets/settings/measure/pillow_settings_scientific_calculation_height_hard_clicked.png',
        select: '../../assets/settings/measure/pillow_settings-unclicked.png',
        selected: '../../assets/settings/measure/pillow_settings-clicked.png',
        text: '较硬',
        index: 2,
      },
    ],
    listHeader: [{
        img: '../../assets/settings/measure/pillow_settings_scientific_calculation_cervical_spine_straighten_unselected.png',
        imgCheck: '../../assets/settings/measure/pillow_settings_scientific_calculation_cervical_spine_straighten_selected.png',
        select: '../../assets/settings/measure/pillow_settings-unclicked.png',
        selected: '../../assets/settings/measure/pillow_settings-clicked.png',
        text: '较软',
        index: 0,
      },
      {
        img: '../../assets/settings/measure/pillow_settings_scientific_calculation_normal_cervical_spine_unselected.png',
        imgCheck: '../../assets/settings/measure/pillow_settings_scientific_calculation_normal_cervical_spine_selected.png',
        select: '../../assets/settings/measure/pillow_settings-unclicked.png',
        selected: '../../assets/settings/measure/pillow_settings-clicked.png',
        text: '中等',
        index: 1,
      },
      {
        img: '../../assets/settings/measure/pillow_settings_scientific_calculation_normal_cervical_spine_unselected_cervical_spine_reverse_bow_unselected.png',
        imgCheck: '../../assets/settings/measure/pillow_settings_scientific_calculation_cervical_spine_reverse_bow_selected.png',
        select: '../../assets/settings/measure/pillow_settings-unclicked.png',
        selected: '../../assets/settings/measure/pillow_settings-clicked.png',
        text: '较硬',
        index: 2,
      },
    ],
  },

  lifetimes: {
    attached: function () {
      // 在组件实例进入页面节点树时执行
    },
    ready: function () {
      // 在组件在视图层布局完成后执行
    }
  },

  methods: {
    handleInput(e) {
      let {
        type
      } = e.currentTarget.dataset;
      const {
        value
      } = e.detail;
      // if (type == 'jjjNum') {
      // 1. 过滤非法字符（只保留数字和第一个小数点）
      let values = value.replace(/[^\d.]/g, '') // 去除非数字和小数点[2,4](@ref)
        .replace(/(\.{2,})/g, '.') // 多个小数点替换为一个[1](@ref)
        .replace(/(\..*)\./g, '$1'); // 保留第一个小数点
      // 2. 限制小数点后一位
      const reg = /^(\d+)(\.\d{0,1})?$/; // 允许整数或一位小数[1,2](@ref)
      if (!reg.test(value)) {
        values = value.substring(0, value.length - 1); // 删除最后一位非法字符
      }
      // 3. 更新数据（避免直接setData导致光标跳转）
      let {
        currentList
      } = this.data;
      currentList[type] = values;
      this.setData({
        currentList,
      });
      // }
    },
    handleWeight(e) {
      let {
        type
      } = e.currentTarget.dataset;
      const {
        value
      } = e.detail;
      // if (type == 'jjjNum') {
      // 1. 过滤非法字符（只保留数字和第一个小数点）
      let values = value.replaceAll(/[^\d.]/g, '') // 去除非数字和小数点[2,4](@ref)
      // 2. 限制小数点后一位
      const reg = /^(\d+)?$/; // 允许整数或一位小数[1,2](@ref)
      if (!reg.test(value)) {
        values = value.substring(0, value.length - 1); // 删除最后一位非法字符
      }
      // 3. 更新数据（避免直接setData导致光标跳转）
      let {
        currentList
      } = this.data;
      currentList[type] = values;
      this.setData({
        currentList,
      });
      // }
    },
    checkRange(e) {
      let {
        type
      } = e.currentTarget.dataset;
      if (type == 'jjjNum') {
        let value = parseFloat(e.detail.value);
        if (isNaN(value) || value < 7 || value > 16) {
          wx.showToast({
            title: '请输入7-16之间的值',
            icon: 'none'
          });
          this.setData({
            'currentList.jjjNum': ''
          }); // 清空非法输入
        } else {
          // 格式化显示（如8.0显示为8）
          this.setData({
            'currentList.jjjNum': value % 1 === 0 ? value.toString() : value.toFixed(1)
          });
        }
      }else if(type=='twNum'){
        let value = parseFloat(e.detail.value);
        if (isNaN(value) || value < 2 || value > 7) {
          wx.showToast({
            title: '请输入2-7之间的值',
            icon: 'none'
          });
          this.setData({
            'currentList.twNum': ''
          }); // 清空非法输入
        } else {
          // 格式化显示（如8.0显示为8）
          this.setData({
            'currentList.twNum': value % 1 === 0 ? value.toString() : value.toFixed(1)
          });
        }
      }else if(type=='jwNum'){
        let value = parseFloat(e.detail.value);
        if (isNaN(value) || value < 3 || value > 8) {
          wx.showToast({
            title: '请输入3-8之间的值',
            icon: 'none'
          });
          this.setData({
            'currentList.jwNum': ''
          }); // 清空非法输入
        } else {
          // 格式化显示（如8.0显示为8）
          this.setData({
            'currentList.jwNum': value % 1 === 0 ? value.toString() : value.toFixed(1)
          });
        }
      }else if(type=='ywjbNum'){
        let value = parseFloat(e.detail.value);
        if (isNaN(value) || value < 2 || value > 7) {
          wx.showToast({
            title: '请输入2-7之间的值',
            icon: 'none'
          });
          this.setData({
            'currentList.ywjbNum': ''
          }); // 清空非法输入
        } else {
          // 格式化显示（如8.0显示为8）
          this.setData({
            'currentList.ywjbNum': value % 1 === 0 ? value.toString() : value.toFixed(1)
          });
        }
      }else if(type=='ywtbNum'){
        let value = parseFloat(e.detail.value);
        if (isNaN(value) || value < 2 || value > 7) {
          wx.showToast({
            title: '请输入2-7之间的值',
            icon: 'none'
          });
          this.setData({
            'currentList.ywtbNum': ''
          }); // 清空非法输入
        } else {
          // 格式化显示（如8.0显示为8）
          this.setData({
            'currentList.ywtbNum': value % 1 === 0 ? value.toString() : value.toFixed(1)
          });
        }
      }else if(type=='cwjbNum'){
        let value = parseFloat(e.detail.value);
        if (isNaN(value) || value < 2 || value > 7) {
          wx.showToast({
            title: '请输入2-7之间的值',
            icon: 'none'
          });
          this.setData({
            'currentList.cwjbNum': ''
          }); // 清空非法输入
        } else {
          // 格式化显示（如8.0显示为8）
          this.setData({
            'currentList.cwjbNum': value % 1 === 0 ? value.toString() : value.toFixed(1)
          });
        }
      }else if(type=='cwtbNum'){
        let value = parseFloat(e.detail.value);
        if (isNaN(value) || value < 2 || value > 7) {
          wx.showToast({
            title: '请输入2-7之间的值',
            icon: 'none'
          });
          this.setData({
            'currentList.cwtbNum': ''
          }); // 清空非法输入
        } else {
          // 格式化显示（如8.0显示为8）
          this.setData({
            'currentList.cwtbNum': value % 1 === 0 ? value.toString() : value.toFixed(1)
          });
        }
      }else if(type=='weightNum'){
        let value = parseFloat(e.detail.value);
        if (isNaN(value) || value < 35 || value > 100) {
          wx.showToast({
            title: '请输入35-100之间的值',
            icon: 'none'
          });
          this.setData({
            'currentList.weightNum': ''
          }); // 清空非法输入
        }else{
          this.setData({
            'currentList.weightNum': value,
          });
        }
      }
    },
    //选择床垫的硬度
    selectFn(item) {
      let index = item.currentTarget.dataset.item;
      this.setData({
        currentIndex: index,
      })
    },
    // 脊椎曲度
    selectHeaderFn(item) {
      let index = item.currentTarget.dataset.item;
      this.setData({
        currentHeaderIndex: index,
      })
    },
    setMeasureFn(){
      //计算仰卧和侧卧高度
      let {currentList,currentIndex,currentHeaderIndex} = this.data;
      if(!currentList.jjjNum){
        wx.showToast({
          title: '请输入肩颧距测量结果',
          icon: 'none'
        })
        return
      }else if(!currentList.twNum){
        wx.showToast({
          title: '请输入头窝测量结果',
          icon: 'none'
        })
        return
      }else if(!currentList.jwNum){
        wx.showToast({
          title: '请输入颈窝测量结果',
          icon: 'none'
        })
        return
      }else if(!currentList.ywjbNum){
        wx.showToast({
          title: '请输入仰卧测量结果',
          icon: 'none'
        })
        return
      }else if(!currentList.ywtbNum){
        wx.showToast({
          title: '请输入仰卧头部测量结果',
          icon: 'none'
        })
        return
      }else if(!currentList.cwjbNum){
        wx.showToast({
          title: '请输入侧卧颈部测量结果',
          icon: 'none'
        })
        return
      }else if(!currentList.cwtbNum){
        wx.showToast({
          title: '请输入侧卧头部测量结果',
          icon: 'none'
        })
        return
      }else if(!currentList.weightNum){
        wx.showToast({
          title: '请输入体重',
          icon: 'none'
        })
        return
      }
      //开始计算
      let ywjb = null;
      let ywtb = null;
      let cwjb = null;
      let cwtb = null;
      //这个是体重
      let weightNum = currentList.weightNum;
      
      ywjb = (Number(currentList.ywjbNum)+1.5+(currentIndex==0?-0.5:0)+(currentHeaderIndex==0?+1:currentHeaderIndex==1?+1.5:0)).toFixed(1);
      ywtb = (Number(currentList.ywtbNum)+1.5+(currentIndex==0?-0.5:0)).toFixed(1);
      cwjb = (Number(currentList.cwjbNum)*0.7+1.5+(currentIndex==0?-0.5:0)).toFixed(1);
      cwtb = (cwjb-3).toFixed(1);
      // 直接把数据组装好
      let setCon = `SET:06;${ywtb*10};${ywjb*10};${cwtb*10};${cwjb*10};${weightNum};00;@`
      this.triggerEvent('setMeasureFn',setCon);
    },
  }
})